package com.family.pedigree.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.family.pedigree.domain.FamilyTree;
import com.family.pedigree.domain.FamilyTreeNode;
import com.family.pedigree.domain.vo.FamilyRankVo;
import com.family.pedigree.mapper.FamilyTreeMapper;
import com.family.pedigree.mapper.FamilyTreeNodeMapper;
import com.family.pedigree.service.IFamilyTreeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 家族树 服务实现类
 * </p>
 *
 * @author forever
 * @since 2024-11-24
 */
@Service
@RequiredArgsConstructor
public class FamilyTreeServiceImpl extends ServiceImpl<FamilyTreeMapper, FamilyTree> implements IFamilyTreeService {

    private final FamilyTreeNodeMapper nodeMapper;

    @Override
    public void add(FamilyTree tree) {
        save(tree.setCreateBy("admin").setCreateTime(LocalDateTime.now()).setUpdateBy("admin").setUpdateTime(LocalDateTime.now()));
    }

    @Override
    public void edit(FamilyTree tree) {
        updateById(tree.setUpdateBy("admin").setUpdateTime(LocalDateTime.now()));

    }

    @Override
    public List<?> getList(FamilyTree tree) {
        return lambdaQuery()
                .eq(tree.getId() != null, FamilyTree::getId, tree.getId())
                .eq(StringUtils.isNotBlank(tree.getFamilyName()), FamilyTree::getFamilyName, tree.getFamilyName())
                .eq(FamilyTree::getIsDelete, 0)
                .orderByDesc(FamilyTree::getCreateTime)
                .list();
    }

    @Override
    public List<FamilyRankVo> getRankList() {
        List<FamilyTreeNode> familyTreeNodes = nodeMapper.selectList(null);
        List<FamilyRankVo> rankVos = new ArrayList<>();
        if (CollUtil.isEmpty(familyTreeNodes)) return rankVos;
        Map<String, List<FamilyTreeNode>> familiyNameMap = familyTreeNodes.stream().collect(Collectors.groupingBy(FamilyTreeNode::getFamilyName));
        familiyNameMap.forEach((k, v) -> {
            FamilyRankVo rankVo = new FamilyRankVo();
            rankVo.setFamilyName(k);
            Map<String, List<FamilyTreeNode>> rankMap = v.stream().collect(Collectors.groupingBy(FamilyTreeNode::getFamilyRank));
            rankMap.forEach((k1, v1) -> {
                rankVo.setFamilyRank(k1);
                rankVo.setNodeList(v1);
                rankVos.add(rankVo);
            });
        });
        return rankVos;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void delByFamilyName(String familyName) {
        lambdaUpdate().set(FamilyTree::getIsDelete, 1).eq(FamilyTree::getFamilyName, familyName).update();
        nodeMapper.updateBatchByFamilyName(familyName);
    }
}
